<?php
namespace App\Ext\Alidayu;

use Flc\Alidayu\Client;
use Flc\Alidayu\App;
use Flc\Alidayu\Requests\AlibabaAliqinFcSmsNumSend;

/*
 * 说明：依赖扩展vendor/flc
 * 使用
 * use App\Ext\Alidayu\AlidayuSms;
 * $sms = new AlidayuSms($app_key, $app_secret);
 * $sms->sms_send($code, $number, $sign, $template);
 */
class AlidayuSms extends Controller
{
    private $config; // 配置文件
    private $code; // 短信模板中的验证码变量
    private $number; // 发送电话号码
    private $sign; // 签名
    private $template; // 模板名

    //构造
    function __construct($app_key, $app_secret)
    {
        $this->config = [
            'app_key' => $app_key,
            'app_secret' => $app_secret
        ];
    }

    public function sms_send($code, $number, $sign, $template) {
        $this->code = $code;
        $this->number = $number;
        $this->sign = $sign;
        $this->template = $template;

        $client = new Client(new App($this->config));
        $req = new AlibabaAliqinFcSmsNumSend;

        $req->setRecNum($this->number)
            ->setSmsParam([
                $this->code => rand(100000, 999999)
            ])
            ->setSmsFreeSignName($this->sign)
            ->setSmsTemplateCode($this->template);

        $resp = $client->execute($req);

        return $resp;
    }

}
